package com.coco.controller;

import com.coco.config.JwtProperties;
import com.coco.entity.UserInfo;
import com.coco.model.NaviInfo;
import com.coco.model.NaviInfoCondition;
import com.coco.model.VideoCondition;
import com.coco.model.VideoModel;
import com.coco.service.FacadeService;
import com.coco.utils.CoPageResp;
import com.coco.utils.CookieUtils;
import com.coco.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author GHOSTLaycoo
 * @date 2020/4/16 0016 - 上午 10:41
 */
@EnableConfigurationProperties(JwtProperties.class)
@RestController
@RequestMapping("facade")
public class FacadeController {

    @Autowired
    private FacadeService FacadeService;

    @Autowired
    private JwtProperties jwtProperties;

    @ApiOperation(value = "获取导航栏数据")
    @PostMapping("/getNaviInfoList")
    public ResponseEntity<List<NaviInfo>> getNaviInfoList(@RequestBody NaviInfoCondition naviInfoCondition,HttpServletRequest request){
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            return ResponseEntity.ok(FacadeService.getNaviInfoList(naviInfoCondition,infoFromToken.getId()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @ApiOperation(value = "获取首页视频数据")
    @PostMapping("/getVideoForOne")
    public ResponseEntity<List<VideoModel>> getVideoForOne(@RequestBody VideoCondition videoCondition){
        return ResponseEntity.ok(FacadeService.getVideoForOne(videoCondition));
    }

    @ApiOperation(value = "获取今日热门视频")
    @GetMapping("/getVideoByToday")
    public ResponseEntity<List<VideoModel>> getVideoByToday(){
        return ResponseEntity.ok(FacadeService.getVideoByToday());
    }

    @ApiOperation(value = "通过id获取视频数据")
    @GetMapping("/getVideoById/{id}")
    public ResponseEntity<VideoModel> getVideoById(@PathVariable("id") String id){
        return ResponseEntity.ok(FacadeService.getVideoById(id));
    }

    @ApiOperation(value = "通过搜索条件获取视频数据")
    @PostMapping("/getVideoByCondition")
    public ResponseEntity<CoPageResp<VideoModel>> getVideoByCondition(@RequestBody VideoCondition videoCondition){
        return ResponseEntity.ok(FacadeService.getVideoByCondition(videoCondition));
    }

}
